8. How to read from and write to files

  1. Motivations
    • Initially all the variables created in a program do not have data, excluding fixed and hard coded data. Up to now, we can read data from the user and save them into variables.
    • All the variables will be destroyed when the program ends.
    • How to use data that were prepared in other ways, without reading from the user, i.e., typing on the keyboard?
    • Another question is how to permanently keep the data even when the program ends, so that the data can be used later or used for other purposes?
  2. Files - A unit storage space on storage media, such as HDDs and memory sticks. Note that a variable is generally a unit storage space in computer main memories.
    • Files have paths and names. E.g., On a Windows computer, C:\Users\mlee\Desktop\Tmp\ttt.py; On a Ubuntu Linux computer, /home2/mlee/Tmp/ttt.py
    • The current working directory (cwd) - the directory in which the program exits.
    • When a data file is used in a program, if the data file exits in the current working directory of the program, then the name of the file can be used in the program without the path of the file.
    • How to use a file
      • Open a file with name [and path if necessary]
      • Read and/or write
      • Close
  3. How to open
    • open(filename, mode) - open a file
      • "r" - reading
      • "w" - writing; existing file will be erased
      • "a" - appending
      • "r+" - reading and writing
      • "rb+" - reading and writing in the binary mode
  4. How to read
    • .read() - reading the entire content
      Let's try the next example. You need to download data0.txt, and upload it to Trinket to test the example by clicking the upward arrow button (Upload text file).
      f = open("data0.txt", "r")  # "r" for reading
      data = f.read()  # read() - read the entire content
      print(data)
      f.close()
      # or
      with open("data0.txt", "r") as f:  # "r" for reading; with the 'with' keyword, file will be automatically closed.
          data = f.read()
          print(data)
      print(f.closed)
      
    • .readline() - reading a line
      Let's try the next example.
      f = open("data0.txt", "r")  # "r" for reading
      data = f.readline()
      print(data)  # is '\n' included in data?
      data = f.readline()
      print(data, end='')
      data = f.readline()
      print(data, end='')
      
    • for line in f: - In the above example, all the 3 lines are ready. But what if we don't know how many lines are stored in a file?
      Let's try the next example.
      f = open("data0.txt", "r")
      for line in f:
          print(line, end='')
      
    • .readlines(): - reading all the lines into a list
      Let's try the next example.
      f = open("data0.txt", "r")
      lines = f.readlines()  # the data type of lines?
      print(lines)
      
    • Let's try the next example that reads numbers from a file and computes the average. Each line has a number, and the first line has an integer that indicates how many numbers are stored. You need to download data1.txt, and upload it to Trinket to test the example.
      f = open("data1.txt", "r")
      howmany = int(f.readline())
      sum = 0
      for i in range(howmany):
          sum = sum + int(f.readline())
      average = sum / howmany
      print(average)
      
  5. How to write
    • First of all we need to open a file with "w" or "a" or "r+".
      .write() - to write a string to a file; not integers nor floats
      Let's try the next example.
      f = open("data3.txt", "w")
      f.write("Why is the universe so huge?\n")  # \n ???
      f.close()
      
    • Let's try the next example that appends a text at the end.
      f = open("data3.txt", "a")
      f.write("Do you know how many starts are in our galaxy, Milky Way?\n")
      f.close()
      
    • Let's try the next example that reads and writes. Reading starts from the beginning, and writing starts from the end.
      f = open("data3.txt", "r+")
      print(f.readline())
      f.write("0. How fast is the light?\n")
      print(f.readline())
      f.write("1. How long does it take to get to the Moon by the light speed?\n")
      f.close()
      
  6. Programming exercises
    • Write a program that reads lists from a file and computes the average for each list. Each line has comma separated numbers, and the first line has an integer that indicates how many lists, i.e., lines of comma separated numbers, are stored. The program saves the data into a list of lists, and prints the list.
      You may download data2.txt to test your program.
      f = open("data2.txt", "r")
      howmany = int(f.readline())
      table = []
      for i in range(howmany):
        line = (f.readline()).rstrip('\n')
        table.append(line.split(','))
      print(table)
      
      averages = []
      for i in range(howmany):
        sum = 0
        for j in range(len(table[i])):
          sum = sum + int(table[i][j])
        averages.append(sum / len(table[i]))
      print(averages)
      
    • Write a program that generates 100 Fibonacci numbers, 1, 1, 2, 3, 5, 8, ..., and saves them to a file.
      f = open("data4.txt", "w")
      f.write('100\n')
      prev0 = 0
      prev1 = 1
      f.write(str(prev1) + '\n')
      for i in range(99):
        current = prev0 + prev1
        f.write(str(current) + '\n')
        prev0 = prev1
        prev1 = current
      
  7. References